part of '../repository.dart';

class _UserDtoFactory {
  const _UserDtoFactory._internal();

  static const _instance = _UserDtoFactory._internal();

  factory _UserDtoFactory() {
    return _instance;
  }

  _UserDto fromJson(Map<String, dynamic> json) {
    final userRoleString = json[_kUserRoleKey] as String;
    final userRoleDto = _UserRoleDto.fromString(userRoleString);
    final userRole = userRoleDto.toEntity();

    if (userRole.isAdmin) {
      return _AdminDto.fromJson(json);
    }

    if (userRole.isEndUser) {
      return _EndUserDto.fromJson(json);
    }

    throw UnimplementedError();
  }

  _UserDto fromEntity(User user) {
    if (user is Admin) {
      return _AdminDto.fromEntity(user);
    }

    if (user is EndUser) {
      return _EndUserDto.fromEntity(user);
    }

    throw UnimplementedError();
  }
}
